package com.attendancesystem.controller;

import com.attendancesystem.config.LocalDateTimeAdapter;
import com.attendancesystem.model.Lecture;
import com.attendancesystem.service.LectureService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.*;



import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.sql.*;
import java.time.LocalDateTime;

@WebServlet(value = "LectureInfoController",name = "LectureInfoController")
public class LectureInfoController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                String courseId = request.getParameter("courseId");
                System.out.println("courseId:" + courseId);

                try {
                    LectureService courseService = new LectureService();
                    Lecture detail = courseService.getLectureDetail(courseId);

                    Gson gson = new Gson();
                    gson=new GsonBuilder()
                            .registerTypeAdapter(LocalDateTime.class, new LocalDateTimeAdapter())
                            .serializeNulls()
                            .create();
                    response.setContentType("application/json");
                    response.getWriter().write(gson.toJson(detail));

                } catch (SQLException e) {
                    response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                }



    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         doGet(request,response);
    }
}
